/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.passport.service.plugins  
 */
package com.apache.passport.service.plugins;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.database.constant.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.passport.entity.UctUser;

/**
 * description:  判断用户状态
 * @author Hou Dayu 创建时间：2015-5-4  
 * @Copyright(c)2014:
 */
public class UserStatePlugin implements PluginConnector {
	protected IDao uctUserDao;

	public void setUctUserDao(IDao uctUserDao) {
		this.uctUserDao = uctUserDao;
	}

	/**
	 * TODO 根据用户名称,判断一下是否已经删除或者账户停用  
	 * @see com.apache.api.manager.PluginConnector#execute(com.apache.api.vo.ParamsVo)  
	 */
	@Override
	public Object execute(ParamsVo arg0) throws Exception {
		ResultEntity result = new ResultEntity();
		result.setResult("true");

		String userEname = (String) arg0.getParams("userEname");
		if (Validator.isNull(userEname)) {
			result.setEntity("F");
			result.setMessage("用户名不能为空!");
			return result;
		}

		UctUser uctUser = (UctUser) getInfoByEname(userEname);

		if (Validator.isEmpty(uctUser)) {
			result.setEntity("F");
			result.setMessage("用户名不存在!");
			return result;
		}
		String delStatus = uctUser.getDelStatus();// 删除状态(F:未删除,T:假删除)
		Integer userStatus = uctUser.getUserStatus();//用户状态(-1:删除状态,0:停用,1:启用)

		if (Validator.isEmpty(userStatus)) {
			userStatus = 10000;//自定值,防止用户状态未设置
		}
		if (Validator.isEmpty(delStatus)) {
			delStatus = "E";//自定值,防止用户删除状态为设置
		}

		if (userStatus == 10000 || "E".equals(delStatus)) {
			result.setEntity("F");
			result.setMessage("用户状态异常,请联系系统管理员!");
			return result;
		}

		if ("T".equals(delStatus) || userStatus == -1) {
			result.setEntity("F");
			result.setMessage("用户已留痕删除,请联系系统管理员!");
			return result;
		}

		if (userStatus == 0) {
			result.setEntity("F");
			result.setMessage("用户已停用,请联系系统管理员!");
			return result;
		}

		result.setEntity("T");
		result.setMessage("账户状态正常!");
		return result;
	}

	/**
	 * description:  根据ename查询用户信息
	 * @param vo
	 * @return  
	 * @author Hou Dayu 
	 * @update 2014-12-19
	 */
	public Object getInfoByEname(String userEname) {
		String sql = uctUserDao.getSql(3);
		MethodParam param = new MethodParam("ByUserEname", "", sql, "com.apache.passport.entity.UctUser");
		param.setParams("userEname", userEname);
		return uctUserDao.selectSingle(param);
	}

}
